﻿using ASmile.Com;
using ASmile.ORM.Entitys;
using System.Collections.Generic;

namespace ASmile.Win
{
    public class BLLCrud<TPageArg, TSaveModel, TListModel> : BLLBase, IBLLCrud<TPageArg, TSaveModel, TListModel>
        where TSaveModel : EntityBase, new()
        where TListModel : class, new()
        where TPageArg : ArgPageData, new()
    {
        public ApiResult<string> Add(TSaveModel model)
        {
            return GetResult<string>(model);
        }

        public ApiResult<string> Update(TSaveModel model)
        {
            return GetResult<string>(model);
        }

        public virtual ApiResult Del(TSaveModel model)
        {
            return GetResult(model);
        }

        public ApiResultList<TListModel> GetList(TPageArg arg)
        {
            return GetResultList<TListModel>(arg);
        }
    }

    public class BLLCrud<TPageArg, TSaveModel> : BLLCrud<TPageArg, TSaveModel, TSaveModel>, IBLLCrud<TPageArg, TSaveModel>
        where TSaveModel : EntityBase, new()
        where TPageArg : ArgPageData, new()
    { }

    public class BLLCrud<TSaveModel> : BLLCrud<ArgPageData, TSaveModel>, IBLLCrud<TSaveModel>
        where TSaveModel : EntityBase, new()
    { }

    public static class BLLCrudExtend
    {
        public static ApiResult<string> Add<TSaveModel>(this BLLCrud<TSaveModel> bll, object model)
            where TSaveModel : EntityBase, new()
        {
            return bll.Add(model as TSaveModel);
        }

        public static List<TListModel> GetList<TListModel>(this BLLCrud<TListModel> bll, int pageNo, int pageSize, string searchVal, out int rowCount)
        where TListModel : EntityBase, new()
        {
            ArgPageData arg = new ArgPageData
            {
                PageNo = pageNo,
                PageSize = pageSize,
                SearchVal = searchVal
            };
            var ret = bll.GetList(arg);
            rowCount = ret.count;
            return ret.data;
        }
    }
}